//
//  HZNetworkTool.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/16.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZNetworkTool.h"
#import "HZDataTool.h"
#import "SQLTools.h"
#import "QuoteLvOper.h"
#import "FileTools.h"
#import "config.h"
#import "HttpTools.h"
#import "HZSearchTool.h"
#import "HZApiTool.h"
QUOTELV_OPER *g_pQuoteOper;

/*数据库*/
sqlite3      *g_pDatabase;
enum QUOTE_STATUS_New_tag{
    QUOTE_STATUS_New_CONN = 0,
    QUOTE_STATUS_New_SERVER_DATA,       //服务器数据
    QUOTE_STATUS_New_SERVER_ERROR,      //服务器错误
    QUOTE_STATUS_New_SERVER_NO_LOGIN,   //没有登录
    QUOTE_STATUS_New_SERVER_NO_LEVEL2,  //没有leve2权限
    QUOTE_STATUS_New_SESSION_TIMEOUT,   //session time out
}QUOTE_STATUS_New;

#define kTypeKey @"HZ_TYPE"
#define kPageNum 10
#define kBlockPageNum 8
#define kPageMoreNum 20

@interface HZNetworkTool()
@property (nonatomic, strong)NSMutableArray *pBindsArray;
@property (nonatomic, assign)NSInteger nQuoteIndex; // 当前行情地址
@end

@implementation HZNetworkTool
- (NSMutableArray *)pBindsArray
{
    if (!_pBindsArray) {
        _pBindsArray = [NSMutableArray array];
    }
    return _pBindsArray;
}

+ (instancetype)shareNetworkTool
{
    static HZNetworkTool *pNetworkTool = nil;
    @synchronized (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            pNetworkTool = [[HZNetworkTool alloc] init];
        });
    }
    
    return pNetworkTool;
}


- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_marketInit];
    }
    return self;
}

- (void)p_marketInit
{
    NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    DLog(@"documentPath==%@",docuPath);
    /*行情初始化*/
    g_pQuoteOper = QuoteLvOper_New();
    QuoteLvOper_SetErrCallBack(g_pQuoteOper, QuoteErrorCallBack, (__bridge void *)(self));
    
    FileTools_Init();
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:[docuPath stringByAppendingPathComponent:@"qver.data"]]) {
        FileTools_UpdateFile("qver.data");
    }
    if (![fileManager fileExistsAtPath:[docuPath stringByAppendingPathComponent:@"gaotime.db"]]) {
        FileTools_UpdateFile("gaotime.db");
    }
//    if (![fileManager fileExistsAtPath:[docuPath stringByAppendingPathComponent:@"config.data"]]) {
        FileTools_UpdateFile("config.data");
//    }
    
    ReadConfig();
    
    //////////////////////////////////////////////////////////////
    _nQuoteIndex = 1;
    char *host = GetConfig("host");
    char *pQU = GetConfig(URL_QUOTE);
    QuoteLvOper_SetNewQuoteServer(g_pQuoteOper, host, QUOTE_SERVER_SOCKET_PORT,pQU,QUOTELV_CONNECTION_SOCKET);
    /////////////////////////////////////////////////////////////
    
    /*打开数据库文件*/
    [self openDatabase];
    
    //请求最新的标的证券的文件
    char * pUrl = GetUrl(URL_BDZQ, 0);
    pUrl = [kUrl_BDZQ UTF8String];
    HttpTools_GetData(0, pUrl, 0, 0, 0, BDZQResedData, (__bridge void *)(self));
    
    QuoteLvOper_GetDictData(g_pQuoteOper, NULL, NULL);
}
void QuoteErrorCallBack(void *pCBObj, void *pError, int nStatus)
{
    HZNetworkTool *spc = (__bridge HZNetworkTool *)pCBObj;
    
    //因为在线和中控制视图动画，会看不到效果，系统会忽略掉事件，这里等系统一点时间。
    //    SLEEP(1000);
    
    switch(nStatus)
    {
        case QUOTE_STATUS_New_SERVER_ERROR:
        {

        }
            break;
        case QUOTE_STATUS_New_SERVER_NO_LOGIN:
        case QUOTE_STATUS_New_SERVER_NO_LEVEL2:
        case QUOTE_STATUS_New_SESSION_TIMEOUT:
        {
            //更新当前服务器地址到leve1上
            //更新行情服务器地址
            char flag[16];
            spc.nQuoteIndex++;
            if (spc.nQuoteIndex++ > 3) {
                return;
            }
            sprintf(flag, "Quote%d",spc.nQuoteIndex);
            char *ipUrl = GetConfig(flag);
            
            NSString *str = [NSString stringWithUTF8String:ipUrl];
            NSURL *url = [NSURL URLWithString:str];
            NSString *ip = url.host;
            QuoteLvOper_SetNewQuoteServer(g_pQuoteOper, ip.UTF8String, QUOTE_SERVER_SOCKET_PORT,ipUrl,QUOTELV_CONNECTION_SOCKET);
            
            Tools_Free(&ipUrl);
            
        }
            break;
    }
}
#pragma mark 数据库控制
- (void)openDatabase
{
    char sPath[MAX_PATH];
    memset(sPath,0,sizeof(sPath));
    
    /*数据库路径*/
    FileTools_GetCurrentPath(sPath);
    strcat(sPath, DATA_BASE);
    
    /*如果Documents目录下没有数据库文件，从根目录下复制过去*/
    FileTools_IsDocuments(sPath);
    
    /*打开数据库文件*/
    if (sqlite3_open(sPath, &g_pDatabase) != SQLITE_OK){
        /*打开失败，关闭数据库*/
        sqlite3_close(g_pDatabase);
    }
}
- (void)closeDatabase
{
    sqlite3_close(g_pDatabase);
}
void BDZQResedData(struct HTTP_TOOLS_tag *pHttp, int nType)
{
    printf("标的证券回调\n");
    HZNetworkTool *spc = (__bridge HZNetworkTool *)pHttp->pCBObj;
    if (nType==HTTP_STATE_FINISH) {
        char *pData = pHttp->pRecvData;
        FileTools_WriteDataEx("bdzq.data", pData, strlen(pData));
    }
}
#pragma mark -
#pragma mark ===============网络相关=================
#warning 做一个订阅最大个数限制
+ (void)networkCacelSubscribeId:(int)Id
{
    QuoteLvOper_CancelSubscribe(g_pQuoteOper, Id);
    NSMutableArray *pTmep = [HZNetworkTool shareNetworkTool].pBindsArray;
    for (NSDictionary *dict in pTmep) {
        if ([dict.allKeys containsObject:[NSNumber numberWithInt:Id]]) {
            [pTmep removeObject:dict];
            return;
        }
    }
}
+ (void)networkCacelAllSubscribeId
{
    QuoteLvOper_CancelAllSubscribe(g_pQuoteOper);
    NSMutableArray *pTmep = [HZNetworkTool shareNetworkTool].pBindsArray;
    [pTmep removeAllObjects];
}

+ (int)networkSubscribeDataWithapiType:(kHZApiType)apiType
                               success:(void(^)(id model, kHZApiType apiType))success
{
   return [self networkSubscribeDataWithapiType:apiType code:nil market:-1 success:success];
}

+ (int)networkSubscribeDataWithapiType:(kHZApiType)apiType
                                  code:(NSString *)code
                                market:(int)market
                               success:(void(^)(id model, kHZApiType apiType))success
{
    return [self networkSubscribeDataWithapiType:apiType code:code market:market kType:-1 success:success];
}

+ (int)networkSubscribeDataWithapiType:(kHZApiType)apiType
                                  code:(NSString *)code
                                market:(int)market
                                 kType:(int)kType
                               success:(void(^)(id model, kHZApiType apiType))success
{
    int nSubId = -1;
    switch (apiType) {
        case kHZApi_Own_Type:
            nSubId = QuoteLvOper_GetOwnData(g_pQuoteOper, NetworkTool_ResedData,  (__bridge void *)([HZNetworkTool shareNetworkTool]), 0, RQS_LV_HSINDEXLIST, 0, TRUE, QUOTELV_OPER_TAG_OWN);
            break;
        case kHZApi_BlockIndex_Type:
            nSubId = QuoteLvOper_GetIndexData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_HSINDEXLIST, 0, TRUE, QUOTELV_OPER_TAG_INDEX);
            break;
        case kHZApi_ZhangFuTop_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1101, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_HS_ZF);
            break;
        case kHZApi_HuanShouTop_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1101, 0, QUOTELV_CUSTOM_HS, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_HS_HS);
            break;
        case kHZApi_LiangBiTop_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1101, 0, QUOTELV_CUSTOM_VR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_HS_LB);
            break;
        case kHZApi_GaiNian_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]),RQS_LV_BORADINDEX,QUOTELV_BLOCK_CONCEPT,0,QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kBlockPageNum, QUOTELV_OPER_TAG_BLOCK_CONCEPT);
            break;
        case kHZApi_HangYe_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]),RQS_LV_BORADINDEX,QUOTELV_BLOCK_INDUSTRY,0,QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kBlockPageNum, QUOTELV_OPER_TAG_BLOCK_INDUSTRY);
            break;
        case kHZApi_Zone_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]),RQS_LV_BORADINDEX,QUOTELV_BLOCK_AREA,0,QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kBlockPageNum,QUOTELV_OPER_TAG_BLOCK_AREA);
            break;
        case kHZApi_ZhangFuTop_CY_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1106, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_CYB_ZF);
            break;
        case kHZApi_HuanShouTop_CY_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1106, 0, QUOTELV_CUSTOM_HS, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_CYB_HS);
            break;
        case kHZApi_LiangBiTop_CY_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1106, 0, QUOTELV_CUSTOM_VR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_CYB_LB);
            break;
        case kHZApi_HuGangTong_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1816, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_HK_GGT);
            break;
        case kHZApi_ShenGangTong_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1813, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_HK_MAIN);
            break;
        case kHZApi_HuGuTong_Type:
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, 1814, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageNum, QUOTELV_OPER_TAG_HK_CY);
            break;
        case kHZApi_ShenGuTong_Type:
            nSubId = QuoteLvOper_GetIndexData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_HKINDEXLIST, 0, TRUE, QUOTELV_OPER_TAG_HK_INDEX);
            break;
        case kHZApi_MinPrice_Type:
            nSubId = QuoteLvOper_GetPriceData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), [code UTF8String], market, 0, TRUE, QUOTELV_OPER_TAG_MIN_PRICE);
            break;
        case kHZApi_MinDetail_Type:
            nSubId = QuoteLvOper_GetDetailData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_DETAIL, [code UTF8String], market, -1, 20, 0, TRUE, QUOTELV_OPER_TAG_DETAIL);
            break;
        case kHZApi_Min_Type:
            nSubId = QuoteLvOper_GetMinData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_MIN, [code UTF8String], market, 0, TRUE, -1, 0, 0, QUOTELV_OPER_TAG_MIN);
            break;
        case kHZApi_IndexZhangFuTop_Type:
        {
            NSInteger blockId = [self p_getBlockIdWithCode:code];
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, blockId, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageMoreNum, QUOTELV_OPER_TAG_INDEX_ZF);
        }
            break;
        case kHZApi_IndexDieFuTop_Type:
        {
            NSInteger blockId = [self p_getBlockIdWithCode:code];
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, blockId, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_DES, 0, TRUE, kPageMoreNum, QUOTELV_OPER_TAG_INDEX_DF);
        }
            break;
        case kHZApi_IndexHuanShouTop_Type:
        {
            NSInteger blockId = [self p_getBlockIdWithCode:code];
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, blockId, 0, QUOTELV_CUSTOM_HS, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageMoreNum, QUOTELV_OPER_TAG_INDEX_HS);
        }
            break;
        case kHZApi_KLine_Type:
        {
            nSubId = QuoteLvOper_GetKData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), kType, [code UTF8String], market, -1, 0, 160, 0, TRUE, QUOTELV_OPER_TAG_KLINE);
        }
            break;
        case kHZApi_BlockDetailList_Type:
        {
            nSubId = QuoteLvOper_GetBlockData(g_pQuoteOper, NetworkTool_ResedData, (__bridge void *)([HZNetworkTool shareNetworkTool]), RQS_LV_BOARDRANK, market, 0, QUOTELV_CUSTOM_DR, QUOTELV_BLOCK_SORT_ASC, 0, TRUE, kPageMoreNum, QUOTELV_OPER_TAG_BLOCK_IAC);
        }
            break;
            
        default:
            break;
    }
    
    [self p_bindSubId:nSubId andType:apiType andSuccess:success];
    return nSubId;
}

+ (void)p_bindSubId:(int)Id
            andType:(kHZApiType)apiType
         andSuccess:(void(^)(id model, kHZApiType apiType))success
{//绑定订阅id和对应的block操作
    
    NSMutableArray *pTempArray = [HZNetworkTool shareNetworkTool].pBindsArray;
    DLog(@"订阅之前存在的id数==%d\n",pTempArray.count);
    for (NSDictionary *dict in pTempArray) {
        if ([dict.allKeys containsObject:kTypeKey]) {
            if ([[dict valueForKey:kTypeKey] integerValue] == apiType) {
                [pTempArray removeObject:dict];
            }
        }
    }
    NSDictionary *pDict = [NSDictionary dictionaryWithObjectsAndKeys:success,[NSNumber numberWithInt:Id],[NSNumber numberWithInteger:apiType],kTypeKey, nil];
    [pTempArray addObject:pDict];
    DLog(@"订阅之后存在的id数//%d\n",pTempArray.count);
}

+ (NSInteger)p_getBlockIdWithCode:(NSString *)aCode
{
    NSInteger blockId = 1102;
    if ([aCode isEqualToString:@"1A0001"]) {//上证okex
        blockId = QUOTELV_TAXIS_SHA;
    }else if ([aCode isEqualToString:@"399001"]) {//深证成指
        blockId = QUOTELV_TAXIS_SZCZ;
    }else if ([aCode isEqualToString:@"399006"]) {//创业板指
        blockId = QUOTELV_TAXIS_CYB;
    }else if ([aCode isEqualToString:@"399005"]) {//中小板指
        blockId = QUOTELV_TAXIS_ZXQY;
    }else if ([aCode isEqualToString:@"1B0300"]) {//BTex300
        blockId = QUOTELV_TAXIS_HS300;
    }else if ([aCode isEqualToString:@"1B0007"]) {//上证180
        blockId = QUOTELV_TAXIS_SH180;
    }else if ([aCode isEqualToString:@"1B0016"]) {//上证50
        blockId = QUOTELV_TAXIS_SBG;
    }else if ([aCode isEqualToString:@"399106"]) {//深证综指(深证A股)
        blockId = QUOTELV_TAXIS_SZA;
    }else if ([aCode isEqualToString:@"1A0002"]) {//A股okex
        blockId = QUOTELV_TAXIS_HSA;
    }else if ([aCode isEqualToString:@"1A0003"]) {//B股okex
        blockId = QUOTELV_TAXIS_HSB;
    }
    
    return blockId;
}
#pragma mark -
#pragma mark ===============网络回调=================
void NetworkTool_ResedData(void *aself, void *p)
{//回调给单例对象
    HZNetworkTool *pNetworkTool = (__bridge HZNetworkTool *)aself;
    printf("NetworkTool_ResedData===%p\n",pNetworkTool);
    QUOTELV_SUBSCRIBE_ITEM * item = (QUOTELV_SUBSCRIBE_ITEM*)p;
    int nBackSubId = item->Id;
    for (NSDictionary *dict in pNetworkTool.pBindsArray) {
        if ([dict.allKeys containsObject:[NSNumber numberWithInt:nBackSubId]]) {
            NSNumber *num = [dict valueForKey:kTypeKey];
            kHZApiType nType = [num integerValue];
            void(^success)(id model, kHZApiType apiType) = [dict objectForKey:[NSNumber numberWithInt:nBackSubId]];
            [HZDataTool dataToModelWithApiType:nType success:success];
            return;
        }
    }
}

#pragma mark -
#pragma mark 自选股
+ (int)networkAddOwnStock:(NSString *)code WithMarketId:(int)marketId
{
    return QuoteLvOper_AddOwnCode(g_pQuoteOper, [code UTF8String], marketId);
}
+ (int)networkDeleteOwnStock:(NSString *)code WithMarketId:(int)marketId
{
    [self speicalDeleteStockCode:code];
    return QuoteLvOper_DelOwnCode(g_pQuoteOper, [code UTF8String], marketId);
}
+ (void)networkDeleteAllOwnStock
{
    QuoteLvOper_DelAllOwnCode(g_pQuoteOper);
}
+ (NSArray *)networkGetAllOwnStock
{
    NSMutableArray *tempArray = [NSMutableArray array];
    
    FILE_DATA* f_data = FileTools_ReadDataEx(g_pQuoteOper->pOwnPath);
    NSInteger nPos = 0;
    for (int i = 0; i < g_pQuoteOper->nOwnMaxCount; i++) {
        nPos = 7*i;
        char temp[8];
        char tempCode[7];
        int marketid;
        memset(temp, 0, 8);
        memset(tempCode, 0, 7);
        memcpy(temp, f_data->pData+nPos, 7);
        memmove(tempCode, temp, 6);
        marketid = temp[6];
        NSDictionary *dict = @{@"code": [NSString stringWithUTF8String:tempCode], @"market": @(marketid)};
        [tempArray addObject:dict];
    }
    FileTools_FreeData(f_data);
    
    return tempArray;
}

+ (BOOL)networkIsOwnStock:(NSString *)code WithMarketId:(int)marketId
{
    return QuoteLvOper_IsOwnCode(g_pQuoteOper, [code UTF8String], marketId);
}
// 为了上传自选股接口特别增加
// 跟新的自选股数据
+ (void)speicalUpdateOwnStocks:(NSArray *)stocks
{
    NSArray *tempArray = [[NSArray alloc] initWithArray:stocks copyItems:YES];
    [MYCommonTool shareToolWriteToFile:@"specialOwnStock.data" content:tempArray];
}
// 添加自选股
+ (void)speicalAddOwnStock:(NSDictionary *)stock
{
    NSString *code = stock[@"code"];
    NSArray *result = [MYCommonTool shareToolReadFileName:@"specialOwnStock.data" readFileType:KReadFileArrayType];
    NSMutableArray *tempArray = [NSMutableArray array];
    [tempArray addObjectsFromArray:result];
    __block BOOL isExist = NO;
    [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *oldModel = (NSDictionary *)obj;
        if ([oldModel[@"code"] isEqualToString:code]) {
            isExist = YES;
            *stop = YES;
        }
    }];
    if (isExist) { return;}
//    [tempArray insertObject:stock atIndex:0];
    [tempArray addObject:stock];
    [MYCommonTool shareToolWriteToFile:@"specialOwnStock.data" content:tempArray];
}
// 删除自选股
+ (void)speicalDeleteStockCode:(NSString *)code
{
    NSArray *result = [MYCommonTool shareToolReadFileName:@"specialOwnStock.data" readFileType:KReadFileArrayType];
    NSMutableArray *tempArray = [NSMutableArray array];
    [tempArray addObjectsFromArray:result];
    [result enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *oldModel = (NSDictionary *)obj;
        if ([oldModel[@"code"] isEqualToString:code]) {
            [tempArray removeObject:oldModel];
            *stop = YES;
        }
    }];
    [MYCommonTool shareToolWriteToFile:@"specialOwnStock.data" content:tempArray];
}
+ (NSArray *)specialGetStocks
{
    NSArray *tempArray = [MYCommonTool shareToolReadFileName:@"specialOwnStock.data" readFileType:KReadFileArrayType];
    return tempArray;
}
+ (void)searchDeleteHistoryStock
{
    [MYCommonTool shareToolWriteToFile:@"specialOwnStock.data" content:[NSArray array]];
    
}

@end
